package com.Offer;

import java.net.ServerSocket;

/*
    面试题5：替换空格
    题目描述：将一个字符串中的空格替换成“%20”。例如：当字符串为 We Are Happy.则经过替换之后的字符串为 We%20Are%20Happy。
    测试用例：We are happy.
 */
public class demo5 {

    public static void main(String[] args) {
        long start = System.currentTimeMillis();

        StringBuffer str = new StringBuffer("we are happy.");
        System.out.println(replaceSpace(str));

        long end = System.currentTimeMillis();
        System.out.println("程序运行时间： "+(end-start)+"ms");

        int[] a = new int[20];
        for(int i = 0;i<10;i++){
            a[i] = 1;
        }
        System.out.println(a[11]);
    }

    public static String replaceSpace(StringBuffer str) {
        if (str == null) return null;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            if (String.valueOf(str.charAt(i)).equals(" ")) {
                sb.append("%20");
            }else {
                sb.append(str.charAt(i));
            }
        }
        return String.valueOf(sb);
    }

    public static int[] combine(int[] A1,int[] A2){
        if(A1.length <= A2.length){
            return null;
        }
        int sum1 = 0,sum2 = 0;
        int i = A1.length-1;
        int j = A2.length-1;
        while(true){
            if(A1[i] == 0){
                sum1++;
                i--;
            }else{
                break;
            }
        }
        while(true){
            if(A2[j] == 0){
                sum2++;
                j--;
            }else{
                break;
            }
        }
        int p1 = A1.length-1-sum1;
        int p2 = A2.length-1-sum2;
        int p3 = p1+p2+1;
        while(true){
            if(A1[p1] >= A2[p2]){
                int t = A1[p3];
                A1[p3] = A1[p1];
                A1[p1] = t;
                p3--;p1--;
            }else{
                int t = A1[p3];
                A1[p3] = A2[p2];
                A2[p2] = t;
                p2--;p3--;
            }
            if(p3 == -1)break;
        }
        return A2;
    }

}
